Skip to content

fix(context): skip source scan for classes without a base class - #11884

Open
zuhairkhalid229 wants to merge 1 commit into
loopbackio:masterfrom
zuhairkhalid229:fix/11878-skip-scan-no-base
Open

zuhairkhalid229 wants to merge 1 commit into
loopbackio:masterfrom
zuhairkhalid229:fix/11878-skip-scan-no-base

Conversation

@zuhairkhalid229

Copy link
Copy Markdown

describeInjectedArguments reads the class source and runs regular expressions to decide whether constructor injections are inherited. It does this even for a class with no base class, where nothing can be inherited.

This adds a hasBaseClass check, so the scan only runs when a base class exists.

I did not use the guard from the issue on its own. Reflect.getMetadata also follows a base class that is linked only through .prototype, so checking Object.getPrototypeOf(target) alone changes the result for those classes. For example, a class wired with Object.setPrototypeOf(A.prototype, Base.prototype) starts to inherit the injections of Base. hasBaseClass uses the same rule as reflect-metadata, and a new test covers this case.

Testing

  • New test: a class with no base class does not have its source read. It fails without this change.
  • New test: a class linked only through .prototype still gets []. It fails with the guard from the issue.
  • New test: a function linked with util.inherits still inherits its injections.
  • @loopback/context: 711 passing. @loopback/core: 342 passing, 4 pending.

I did not run the full monorepo test suite locally, and I did not re-measure the startup numbers from the issue.

Fixes #11878

Checklist

  • DCO (Developer Certificate of Origin) signed in all commits
  • npm test passes on your machine (ran @loopback/context and @loopback/core only, see above)
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • API Documentation in code was updated

describeInjectedArguments reads the class source and runs regular
expressions to decide whether constructor injections are inherited.
This also runs for a class with no base class, where nothing can be
inherited, so applications that inspect many classes pay for it at
startup.

Check for a base class first and scan only when one exists. A class can
be linked to a base class only through its prototype, and
Reflect.getMetadata follows that link, so the check covers this case too.
Results stay the same for such classes.

Fixes loopbackio#11878

Signed-off-by: zuhairkhalid229 <zuhairkhalid229@gmail.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avoid constructor source scanning for classes without a base class in @loopback/context

1 participant